import api from './index'

export interface Contact {
  id: number
  name: string
  avatar: string
  status: 'online' | 'offline'
}

export interface Message {
  id: number
  content: string
  time: Date
  isMine: boolean
  type: 'text' | 'file' | 'image'
  fileInfo?: {
    name: string
    size: number
    url: string
  }
}

// 获取联系人列表
export const getContacts = () => {
  return api.get<Contact[]>('/contacts')
}

// 添加联系人
export const addContact = (username: string) => {
  return api.post<Contact>('/contacts', { username })
}

// 获取聊天记录
export const getMessages = (contactId: number, params: {
  before?: Date
  limit?: number
}) => {
  return api.get<Message[]>(`/messages/${contactId}`, { params })
}

// 发送消息
export const sendMessage = (contactId: number, data: {
  content: string
  type: 'text' | 'file' | 'image'
  fileInfo?: {
    name: string
    size: number
    url: string
  }
}) => {
  return api.post<Message>(`/messages/${contactId}`, data)
}

// 获取未读消息数
export const getUnreadCount = () => {
  return api.get<{ [key: number]: number }>('/messages/unread')
}

// 标记消息为已读
export const markAsRead = (contactId: number) => {
  return api.put(`/messages/${contactId}/read`)
} 